﻿<!DOCTYPE html>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>远程文件下载</title>
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="Keywords" content="{config[keyword]}">
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
	<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body >
<div data-role="page">
	<div data-role="header">
		<h1>远程文件辅助下载</h1>
	</div>
	<div data-role="content">
		<form method="post">
		<label for="url">下载地址：</label>
		<input name="url" size="40" id="url" data-mini="true"/>
		<label for="password">管理员密码：</label>
		<input name="password" id="password" data-mini="true"/>
		<input name="submit" type="submit" value="开始" data-role="button" data-theme="b"/>
		</form>
	</div>
<?php
$password = '2876254';//管理密码
$destination_folder = './down/';// 下载的文件保存目录。必须以斜杠结尾

set_time_limit (0); //不限时 24 * 60 * 60
$pass = $_POST['password'];
if ($pass == $password) {
	class runtime {
		var $StartTime = 0;
		var $StopTime = 0;
		function get_microtime(){
			list($usec, $sec) = explode(' ', microtime());
			return ((float)$usec + (float)$sec);
		}
		function start() {
			$this->StartTime = $this->get_microtime();
		}
		function stop() {
			$this->StopTime = $this->get_microtime();
		}
		function spent() {
			return round(($this->StopTime - $this->StartTime) * 1000, 1);
		}
	}
	$runtime= new runtime;
	$runtime->start();
	
	if (!isset($_POST['submit'])){
		die();
	}
	if(!is_dir($destination_folder)) //判断目录是否存在
	mkdir($destination_folder,0777); //若无则创建，并给与777权限 windows忽略
	$url = $_POST['url'];
	$headers = get_headers($url, 1); //得到文件大小
	if ((!array_key_exists("Content-Length", $headers))) {
		$filesize=0;
	}
	$newfname = $destination_folder . basename($url);
	$file = fopen ($url, "rb");
	if ($file) {
		$newf = fopen ($newfname, "wb");
		if ($newf){
			while(!feof($file)) {
				fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
			}
		}
	}
	if($file){
		fclose($file);
	}
	if($newf){
		fclose($newf);
	}
	$runtime->stop();
	
	echo '<br /><li>下载耗时:<font color="blue"> '.$runtime->spent().' </font>微秒,文件大小<font color="blue"> '.$headers["Content-Length"].' </font>字节</li>';
	echo '<br /><li><font color="green">下载成功！请登录FTP下载！ '.$showtime=date("Y-m-d H:i:s").'</font></li>';
}elseif(isset($_POST['password'])){
	echo '<br /><li><font color="red">密码错误！请从新输入密码!</font></li>';
}
?>
</div>
</body>
</html> 